package com.faryangsh.algorithm.basicfor1;


import org.junit.Assert;

/**
 * Created by 杨志远 on 2017/8/13
 * email faryangsh@163.com.
 */
//TODO 迭代在1.4
public class StackInNode<T> {
    private int size;
    private Node top;

    private class Node {
        T item;
        Node next;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public void push(T t) {
        Node oldNode = top;
        top = new Node();
        top.item = t;
        top.next = oldNode;
        size++;
    }

    public T pop() {
        Assert.assertTrue("stack have no elements", size == 0);
        T result = top.item;
        top = top.next;
        size--;
        return result;
    }

    public static void main(String[] args) {
        StackInNode<String> stack = new StackInNode<>();
        stack.push("1");
        stack.push("2");
        stack.push("3");
        System.out.print(stack.pop());
        System.out.print(stack.pop());
        System.out.print(stack.pop());
        System.out.print(stack.pop());
    }
}

